import {
  selectOcrList,
  ocrInfoDetail,
  selectDriverPaperInfo,
  saveDriverPaper,
  selectFileList,
  saveDriverPaperInfo,
  deletePartsInfo,
  auditOcr,
  deleteOcr,
  commitOcr,
  ocrPapersList,
  ocrPapersInfo,
  discernOcrPapers,
  saveOcrPapers,
  delOcrPapers
} from '@/services/supplierManage/paperVerify';
import prompt from '@/components/Prompt';
import { getPageSize } from '@/utils/common';

export default {
  namespace: 'paperVerify',

  state: {
    ocrList: {}, // 司机证件列表
    ocrDetails: {}, // 司机证件详情
    formValues: {},
    ocrPeperList: {},
    ocrPapersDetail: {}
  },

  effects: {
    // 一、OCR
    //  1 查询OCR列表
    *selectOcrList({ payload, callback }, { call, put }) {
      const response = yield call(selectOcrList, payload);
      const { code, data } = response;
      let pagination = { list: [], pageSize: getPageSize(), total: 0, pageNum: 1 };
      if (code === 0) {
        pagination = { ...pagination, ...data };
      }
      const { pageNum, pageSize, total, list } = pagination;
      yield put({
        type: 'saveAllValus',
        payload: {
          ocrList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    //  2、查询OCR详情
    *ocrInfoDetail({ payload, callback }, { call, put }) {
      const response = yield call(ocrInfoDetail, payload);
      const { code, data } = response;
      let newData = [];
      if (code === 0) {
        newData = data;
      }
      yield put({
        type: 'detail',
        payload: { ocrDetails: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    //  3、OCR审核 
    *auditOcr({ payload, callback }, { call }) {
      const response = yield call(auditOcr, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `审核${message}!` });
        if (callback) callback(data);
      }
    },
    //  4、OCR提交
    *commitOcr({ payload, callback }, { call }) {
      const response = yield call(commitOcr, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `提交${message}!` });
        if (callback) callback(data);
      }
    },
    //  5、OCR删除 
    *deleteOcr({ payload, callback }, { call }) {
      const response = yield call(deleteOcr, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `删除${message}!` });
        if (callback) callback(data);
      }
    },

    // 二、OCR证件信息
    // 1、证件列表
    *ocrPapersList({ payload, callback }, { call, put }) {
      const response = yield call(ocrPapersList, payload);
      const { code, data } = response;
      const list = { list: data || [] }
      yield put({
        type: 'detail',
        payload: { ocrPeperList: { [payload.id]: list } },
      });
      callback && callback(data);
    },
    // 2、OCR证件详情   
    *ocrPapersInfo({ payload, callback }, { call, put }) {
      const response = yield call(ocrPapersInfo, payload);
      const { code, data } = response;
      let newData = {};
      if (code === 0) {
        newData = data;
        const filePayLoad = { bizId: payload.id, fileBizType: 'OCR' };
        const fileResponse = yield call(selectFileList, filePayLoad);
        if (fileResponse.code === 0) {
          newData.fileList = fileResponse.data;
        } else {
          newData.fileList = [];
        }
      }
      yield put({
        type: 'saveAllValus',
        payload: { ocrPapersDetail: newData },
      });
      callback && callback(newData);
    },
    // 3、证件识别：  
    *discernOcrPapers({ payload, callback }, { call, put }) {
      const response = yield call(discernOcrPapers, payload);
      const { code, data } = response;
      let newData = {};
      if (code === 0) {
        newData = data;
        const filePayLoad = { bizId: data.papersId, fileBizType: 'OCR' };
        const fileResponse = yield call(selectFileList, filePayLoad);
        if (fileResponse.code === 0) {
          newData.fileList = fileResponse.data;
        } else {
          newData.fileList = [];
        }
        yield put({
          type: 'saveAllValus',
          payload: { ocrPapersDetail: newData },
        });
        callback && callback(newData);
      }

    },
    //4、OCR证件保存：
    *saveOcrPapers({ payload, callback }, { call }) {
      const response = yield call(saveOcrPapers, payload);
      const { code, data, message } = response;
      const content = `保存${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    //5、OCR证件删除：
    *delOcrPapers({ payload, callback }, { call }) {
      const response = yield call(delOcrPapers, payload);
      const { code, data, message } = response;
      const content = `删除${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
  },
};
